﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Report
{
    public partial class ListForm : Form
    {
        private IList<IItem> categoryItems;
        private ReportManager reportManager;
        public ListForm()
        {
            InitializeComponent();

            reportManager = new ReportManager();
            Initialize();
        }
     
        public ListForm(IList<IItem> datas):this()
        {
            reportManager.InitializeGridView(dataGridView1, datas);
        }

        private void Initialize()
        {
            radioButtonClear.Checked = true;
            categoryItems = reportManager.BuildDetailList();

          //  categoryItems = reportManager.BuildFlatList(DbProvider.Data).Where(r => r.Name != "根目录").ToList();
            comboBox1.DataSource = reportManager.GetCategoryNames();
            comboBox1.SelectedIndex = -1;
            reportManager.InitializeGridView(dataGridView1, categoryItems);
        }
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
                return;
            var list = categoryItems.Where(r=>r.FirstLevel == comboBox1.SelectedValue.ToString() 
                || r.SecondLevel == comboBox1.SelectedValue.ToString());
            reportManager.InitializeGridView(dataGridView1, list.ToList());

        }

        private void showAllbutton_Click(object sender, EventArgs e)
        {
            Initialize();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null && e.Value.ToString() == DateTime.MinValue.Date.ToString())
            {
                e.Value = string.Empty;
            }
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "csv files (*.csv)|*.csv";
            saveFileDialog1.Title = "Export in CSV format";
            saveFileDialog1.ShowDialog();
            var headers = new List<string> {"科目",	 
                "子科目"	,"时间","内容提要",	"收入",	"支出",	"现有余额","应有余额" ,	"借入款（减）",	"借出款（加）","备注"};
            if (!string.IsNullOrEmpty(saveFileDialog1.FileName))
            {
                new CSVGenerator().ExportToCSVFile(categoryItems, headers, saveFileDialog1.FileName);
            }
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            var openfile = new OpenFileDialog();
            openfile.Filter = "csv files (*.csv)|*.csv";
            openfile.ShowDialog();
            if (!string.IsNullOrEmpty(openfile.FileName))
            {
                reportManager.SaveToDB(reportManager.ReadCsvFile(openfile.FileName), radioButtonAppend.Checked);
                MessageBox.Show("导入成功");
                Initialize();
            }

        }

        private void buttondelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in  dataGridView1.SelectedRows)
            {
                var index =row.Index;
                categoryItems.RemoveAt(index);
            }
         
            reportManager.InitializeGridView(dataGridView1, categoryItems);
  
          
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            reportManager.SaveToDb();
            MessageBox.Show("保持成功");
            Initialize();
        }


      
    }
}
